package com.zhiyou100.aspect;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import com.zhiyou100.model.User;
import com.zhiyou100.service.LogService;
import com.zhiyou100.util.MyLogAnno;

@Component
@Aspect
public class MyLogAspect {
	@Autowired
	private LogService logservice;
	
	// 使用后置返回通知
	@AfterReturning("@annotation(com.zhiyou100.util.MyLogAnno)")
	public void after(JoinPoint joinPoint){
		System.out.println("后置通知");
		// 获得注解中的值
		String desc = getAnnoDesc(joinPoint);
		System.out.println("请求的方法:"+desc);
		// 以下代码需要使用Springmvc,既控制层也需要由Spring托管,才会获得ip
		//HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
		HttpServletRequest request = servletRequestAttributes.getRequest();
        //ip
        String ip = request.getRemoteAddr();
		System.out.println("请求对象的ip:"+ip);
		
		// 获得用户信息
		User users = (User)request.getSession().getAttribute("user");
		Integer id = users.getId();
		String name = users.getName();
		String ipone = users.getIpone();
		String rname = users.getrName();
		String user = "id:"+id+" name: "+name+" rName: "+rname+" ipone: "+ipone;
//		User user = (User) request.getSession().getAttribute("user");
		System.out.println("请求对象的信息 : "+user);
		Date date = new Date();
		String partten = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(partten);
		String format = sdf.format(date);
		System.out.println("时间 :"+format);
		Map<String,Object> map = new HashMap<>();
		map.put("name", name);
		map.put("handle", desc);
		map.put("ip", ip);
		map.put("user", user);
		map.put("time", format);
		 logservice.addLogs(map);
		
	}
	/*
	 * 封装的方法,获得目标方法上的注解的值
	 */
	public static String getAnnoDesc(JoinPoint joinPoint){
		String desc = "";
		// 获得目标方法名
		String methodName = joinPoint.getSignature().getName();
		// 获得目标方法的参数
		Object[] args = joinPoint.getArgs();
		// 获得目标方法的全部方法对象
		Method[] methods = joinPoint.getTarget().getClass().getDeclaredMethods();
		// 如果该类中的方法与目标方法名一致
		for (Method method : methods) {
			Class<?>[] types = method.getParameterTypes();
			if (method.getName().equals(methodName)) {
				// 判断参数个数是否一致,防止是重载的方法
				if (args.length == types.length) {
					desc = method.getAnnotation(MyLogAnno.class).value();
				}
			}
		}
		return desc;
	}

	
	
	
}
